package cn.oowoo.rocketmqnative.test1;

import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;

public class Producer {
    public static void main(String[] args) {
        DefaultMQProducer producer = new DefaultMQProducer("oowoo_group");//创建生产者分组
        producer.setNamesrvAddr("localhost:9876");//多个地址食用分号隔开
        try {
            producer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
        for(int i=0;i<100;i++){
            Message message = new Message("oowoo_group_test1_topic","tag1",("Hello RocketMQ-->"+i).getBytes(StandardCharsets.UTF_8));
            try {
                Thread.sleep(100);
                SendResult sendResult = producer.send(message);
                System.out.println(JSON.toJSONString(sendResult));
            } catch (MQClientException e) {
                e.printStackTrace();
            } catch (RemotingException e) {
                e.printStackTrace();
            } catch (MQBrokerException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //关闭消费者
        producer.shutdown();
    }
}
